package com.acme.controller;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.acme.dao.ClaimDAO;
import com.acme.dao.UserDAO;
import com.acme.dao.security.DBUserDAO;
import com.acme.entity.User;
import com.acme.entity.security.DBAuthority;

@Transactional
@Controller
@RequestMapping(value = "/")
public class AdminController {
	
	@Autowired
	private ClaimDAO claimDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private DBUserDAO dbUserDAO;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired @Qualifier("accountEnabledEmail")
	private SimpleMailMessage accountEnabledEmail;
	
	@Autowired @Qualifier("accountDisabledEmail")
	private SimpleMailMessage accountDisabledEmail;
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "Admin/Disable/{userID}", method = RequestMethod.GET)
		public String disable(@PathVariable Integer userID) {
		User user = userDAO.getUserByID(userID);
		user.setEnabled(false);
		userDAO.updateUser(user);
		sendNotificationEmail(accountDisabledEmail, user);
		return "redirect:/AdminUsers";
	}
	
	@RequestMapping(value = "Admin/Enable/{userID}", method = RequestMethod.GET)
		public String enable(@PathVariable Integer userID) {
		User user = userDAO.getUserByID(userID);
		user.setEnabled(true);
		userDAO.updateUser(user);
		sendNotificationEmail(accountEnabledEmail, user);
		return "redirect:/AdminUsers";
	}
	
	@RequestMapping(value = "Admin/Roles/{userID}", method = RequestMethod.GET)
	public String roleAction(
							@PathVariable Integer userID, 
							@RequestParam(value = "action", required = true) String action,
							@RequestParam(value = "role", required = true) String role) {
							
	if(action.equals("add")&&!role.equals("")) {
		DBAuthority authority = new DBAuthority(role, userID);
		dbUserDAO.addAuthority(authority);
	}
	
	if(action.equals("remove")&&role!=""&&role!=null) {
		dbUserDAO.removeAuthority(Integer.parseInt(role));
	}
			
	return "redirect:/AdminUsers";
	}
	
	@RequestMapping(value = "AdminAllClaims", method = RequestMethod.GET)
	public ModelAndView AdminAllClaims(){
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("AllClaims", claimDAO.getAllClaims());
		return model;
	}
	
	@RequestMapping(value = "AdminAcceptedClaims", method = RequestMethod.GET)
	public ModelAndView AdminAcceptedClaims(){
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("AcceptedClaims", claimDAO.getAllAcceptedClaims());
		return model;
	}
	
	@RequestMapping(value = "AdminRejectedClaims", method = RequestMethod.GET)
	public ModelAndView AdminRejectedClaims(){
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("RejectedClaims", claimDAO.getAllRejectedClaims());
		
		return model;
	}
	
	@RequestMapping(value = "AdminUsers", method = RequestMethod.GET)
	public ModelAndView AdminUsers(){
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("Users", userDAO.getAllUsers());
		
		return model;
	}
	
	private void sendNotificationEmail(SimpleMailMessage unformattedEmail, User user) {
		SimpleMailMessage newMessage = new SimpleMailMessage();
		newMessage.setFrom(unformattedEmail.getFrom());
		newMessage.setTo(user.getEmail());
		newMessage.setSubject(unformattedEmail.getSubject());
		String name = user.getfName() + " " + user.getlName();
		newMessage.setText(String.format(unformattedEmail.getText(), name));
		mailSender.send(newMessage);
	}
}
